package com.epiboly.base.config;


import cn.hutool.core.date.DatePattern;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * FastJson config
 *
 * @author lshaci
 */
@Slf4j
@Configuration
public class WebConfig {

    /**
     * Fast Json Http Message Converter
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        Set<MediaType> supportedMediaTypes = new HashSet<>();

        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);

        fastJsonHttpMessageConverter.setSupportedMediaTypes(new ArrayList<>(supportedMediaTypes));

        FastJsonConfig fastJsonConfig = fastJsonHttpMessageConverter.getFastJsonConfig();

        fastJsonConfig.setDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.DisableCircularReferenceDetect
        );
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }
}
